package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsKeyword;
import com.qf.service.DtsKeywordService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/24 13:39
 */

@RestController
@RequestMapping("/admin/keyword")
@Slf4j
@CrossOrigin
public class KeywordController {
    @Autowired
    private DtsKeywordService keywordService;

    //   /// http://localhost:8080/admin/keyword/list?page=1&limit=20&keyword=ee&url=eee&sort=add_time&order=desc
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button ="查找")
    @RequiresPermissions("admin:keyword:list")
    @GetMapping("/list")
    public Object KeywordList(Integer page, Integer limit, String order, String sort, String keyword, String url) {
        Page<DtsKeyword> dtsKeywordPage = keywordService.selectPage(page, limit, sort, order, keyword, url);
        long total = dtsKeywordPage.getTotal();
        List<DtsKeyword> records = dtsKeywordPage.getRecords();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button ="添加")
    @RequiresPermissions("admin:keyword:create")
    @PostMapping("create")
    public Object keywordCreate(@RequestBody DtsKeyword dtsKeyword) {
        if(dtsKeyword==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsKeyword.setAddTime(new Date());
        dtsKeyword.setDeleted(false);
        dtsKeyword.setUpdateTime(new Date());
        keywordService.insertKey(dtsKeyword);
        return ResponseUtil.ok(dtsKeyword);
    }
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button ="编辑")
    @RequiresPermissions("admin:keyword:update")
    @PostMapping("update")
    public Object keywordUpdate(@RequestBody DtsKeyword dtsKeyword) {
        if(dtsKeyword==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsKeyword.setUpdateTime(new Date());
        keywordService.updateKey(dtsKeyword);
        return ResponseUtil.ok(dtsKeyword);
    }
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button ="删除")
    @RequiresPermissions("admin:keyword:delete")
    @PostMapping("delete")
    public Object keywordDelete(@RequestBody DtsKeyword dtsKeyword) {
        if(dtsKeyword==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsKeyword.setDeleted(true);
        keywordService.deleteKey(dtsKeyword);
        return ResponseUtil.ok();
    }
}
